package com.zhujiahui.MavenZhuNLP.util;

import javax.swing.*;
import java.io.*;
import java.util.ArrayList;
import java.util.regex.Pattern;

public class FileUtils {

    public static ArrayList<ArrayList<String>> readToString2DLL(String fileName) {

        File tf = new File(fileName);
        BufferedReader br = null;
        ArrayList<ArrayList<String>> dataList = new ArrayList<ArrayList<String>>();
        Pattern pattern = Pattern.compile("[\\t|\\s|,]");

        try {
            br = new BufferedReader(new FileReader(tf));
            String strLine = null;
            // 按行读取
            while ((strLine = br.readLine()) != null) {
                ArrayList<String> al = new ArrayList<String>();
                String[] splitStrings = pattern.split(strLine.trim());
                for (int i = 0; i < splitStrings.length; i++) {
                    al.add(splitStrings[i]);
                }
                dataList.add(al);
            }
            br.close();
        } catch (FileNotFoundException e) {
            //JOptionPane.showMessageDialog(null, "文件没有找到", "错误", JOptionPane.ERROR_MESSAGE);
            e.printStackTrace();
        } catch (IOException e) {
            //JOptionPane.showMessageDialog(null, "文件读取错误", "错误", JOptionPane.ERROR_MESSAGE);
            e.printStackTrace();
        }

        return dataList;
    }


    public static ArrayList<ArrayList<String>> readToString2DLL(String fileName, String delimiter) {
        File tf = new File(fileName);
        BufferedReader br = null;
        ArrayList<ArrayList<String>> dataList = new ArrayList<ArrayList<String>>();

        try {
            br = new BufferedReader(new FileReader(tf));
            String strLine = null;
            // 按行读取
            while ((strLine = br.readLine()) != null) {
                String [] splitStrings = strLine.trim().split(delimiter);
                ArrayList<String> al = new ArrayList<String>();
                for (int i = 0; i < splitStrings.length; i++) {
                    al.add(splitStrings[i]);
                }
                dataList.add(al);
            }
            br.close();
        } catch (FileNotFoundException e) {
            JOptionPane.showMessageDialog(null, "文件没有找到", "错误", JOptionPane.ERROR_MESSAGE);
            e.printStackTrace();
        } catch (IOException e) {
            JOptionPane.showMessageDialog(null, "文件读取错误", "错误", JOptionPane.ERROR_MESSAGE);
            e.printStackTrace();
        }

        return dataList;
    }


    public static double[][] readToDouble2DAA(String fileName, String delimiter) {

        File tf = new File(fileName);
        BufferedReader br = null;
        ArrayList<double []> dataList2 = new ArrayList<double []>();

        try {
            br = new BufferedReader(new FileReader(tf));
            String strLine = null;
            // 按行读取
            while ((strLine = br.readLine()) != null) {
                String [] splitStrings = strLine.trim().split(delimiter);
                double [] doubleValues = new double[splitStrings.length];
                for (int i = 0; i < splitStrings.length; i++) {
                    doubleValues[i] = Double.parseDouble(splitStrings[i]);
                }
                dataList2.add(doubleValues);
            }
            br.close();
        } catch (FileNotFoundException e) {
            JOptionPane.showMessageDialog(null, "文件没有找到", "错误", JOptionPane.ERROR_MESSAGE);
            e.printStackTrace();
        } catch (IOException e) {
            JOptionPane.showMessageDialog(null, "文件读取错误", "错误", JOptionPane.ERROR_MESSAGE);
            e.printStackTrace();
        }

        double[][] dataList = new double[dataList2.size()][];
        for (int i = 0; i < dataList2.size(); i++) {
            dataList[i] = dataList2.get(i);
        }

        return dataList;
    }
}
